package com.tangkun.environment.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.entity.base.CommonReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;
import com.rengwuxian.materialedittext.MaterialEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改密码
 */

public class XiuGaiMiMaActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.et_xiugaishouji_telephone)
    EditText etXiugaishoujiTelephone;
    @BindView(R.id.iv_xiugaishouji_telephone_delete)
    ImageView ivXiugaishoujiTelephoneDelete;
    @BindView(R.id.iv_oldtelephone_xiugaishouji_show_or_hide)
    ImageView ivOldtelephoneXiugaishoujiShowOrHide;
    @BindView(R.id.et_xiugaishouji_newtelephone)
    EditText etXiugaishoujiNewtelephone;
    @BindView(R.id.iv_xiugaishouji_newtelephone_delete)
    ImageView ivXiugaishoujiNewtelephoneDelete;
    @BindView(R.id.iv_newtelephone_xiugaishouji_show_or_hide)
    ImageView ivNewtelephoneXiugaishoujiShowOrHide;
    @BindView(R.id.btn_xiugaishouji_queren)
    Button btnXiugaishoujiQueren;
    @BindView(R.id.et_xiugaishouji_newtelephone_confirm)
    MaterialEditText etXiugaishoujiNewtelephoneConfirm;
    @BindView(R.id.iv_xiugaishouji_newtelephone_confirm_delete)
    ImageView ivXiugaishoujiNewtelephoneConfirmDelete;
    @BindView(R.id.iv_newtelephone_confirm_xiugaishouji_show_or_hide)
    ImageView ivNewtelephoneConfirmXiugaishoujiShowOrHide;

    private Context mContext;
    /**
     * 是否显示旧密码
     */
    private boolean isShowOldTelephone = true;
    /**
     * 是否显示新密码
     */
    private boolean isShowNewTelephone = true;
    /**
     * 是否显示确认密码
     */
    private boolean isShowNewTelephoneConfirm = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_xiugaimima);
        ButterKnife.bind(this);
        title.setText("修改密码");
        initListener();
        showOrHideOldTelephone();
        showOrHideNewTelephone();
        showOrHideNewTelephoneConfirm();
    }

    private void initListener() {
        //监听密码控件删除按钮显示与隐藏
        etXiugaishoujiTelephone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (etXiugaishoujiTelephone.getText().toString().length() > 0) {
                    ivXiugaishoujiTelephoneDelete.setVisibility(View.VISIBLE);
                } else {
                    ivXiugaishoujiTelephoneDelete.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //监听新密码控件删除按钮显示与隐藏
        etXiugaishoujiNewtelephone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (etXiugaishoujiNewtelephone.getText().toString().length() > 0) {
                    ivXiugaishoujiNewtelephoneDelete.setVisibility(View.VISIBLE);
                } else {
                    ivXiugaishoujiNewtelephoneDelete.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //监听确认新密码控件删除按钮显示与隐藏
        etXiugaishoujiNewtelephoneConfirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (etXiugaishoujiNewtelephoneConfirm.getText().toString().length() > 0) {
                    ivXiugaishoujiNewtelephoneConfirmDelete.setVisibility(View.VISIBLE);
                } else {
                    ivXiugaishoujiNewtelephoneConfirmDelete.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    /**
     * 显示与隐藏旧密码
     */
    private void showOrHideOldTelephone() {
        if (isShowOldTelephone) {//当前状态是显示密码，置为隐藏密码
            isShowOldTelephone = false;
            ivOldtelephoneXiugaishoujiShowOrHide.setImageResource(R.mipmap.ic_mima_hide);
            //隐藏旧密码
            etXiugaishoujiTelephone.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {//当前状态是隐藏密码，置为显示密码
            isShowOldTelephone = true;
            ivOldtelephoneXiugaishoujiShowOrHide.setImageResource(R.mipmap.ic_mima_show);
            //显示旧密码
            etXiugaishoujiTelephone.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    /**
     * 显示与隐藏新密码
     */
    private void showOrHideNewTelephone() {
        if (isShowNewTelephone) {//当前状态是显示密码，置为隐藏密码
            isShowNewTelephone = false;
            ivNewtelephoneXiugaishoujiShowOrHide.setImageResource(R.mipmap.ic_mima_hide);
            //隐藏旧密码
            etXiugaishoujiNewtelephone.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {//当前状态是隐藏密码，置为显示密码
            isShowNewTelephone = true;
            ivNewtelephoneXiugaishoujiShowOrHide.setImageResource(R.mipmap.ic_mima_show);
            //显示旧密码
            etXiugaishoujiNewtelephone.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    /**
     * 显示与隐藏确认密码
     */
    private void showOrHideNewTelephoneConfirm() {
        if (isShowNewTelephoneConfirm) {//当前状态是显示密码，置为隐藏密码
            isShowNewTelephoneConfirm = false;
            ivNewtelephoneConfirmXiugaishoujiShowOrHide.setImageResource(R.mipmap.ic_mima_hide);
            //隐藏旧密码
            etXiugaishoujiNewtelephoneConfirm.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {//当前状态是隐藏密码，置为显示密码
            isShowNewTelephoneConfirm = true;
            ivNewtelephoneConfirmXiugaishoujiShowOrHide.setImageResource(R.mipmap.ic_mima_show);
            //显示旧密码
            etXiugaishoujiNewtelephoneConfirm.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }


    @OnClick({R.id.back, R.id.iv_xiugaishouji_telephone_delete, R.id.iv_oldtelephone_xiugaishouji_show_or_hide, R.id.iv_xiugaishouji_newtelephone_delete, R.id.iv_newtelephone_xiugaishouji_show_or_hide, R.id.iv_xiugaishouji_newtelephone_confirm_delete, R.id.iv_newtelephone_confirm_xiugaishouji_show_or_hide, R.id.btn_xiugaishouji_queren})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back://返回按钮
                Utils.hideSoftKeyboard(XiuGaiMiMaActivity.this, back);
                JumpHelper.finish(mContext);
                break;
            case R.id.iv_xiugaishouji_telephone_delete://清空旧密码
                etXiugaishoujiTelephone.setText("");
                break;
            case R.id.iv_oldtelephone_xiugaishouji_show_or_hide://显示与隐藏旧密码
                showOrHideOldTelephone();
                break;
            case R.id.iv_xiugaishouji_newtelephone_delete://清空新密码
                etXiugaishoujiNewtelephone.setText("");
                break;
            case R.id.iv_newtelephone_xiugaishouji_show_or_hide://显示与隐藏新密码
                showOrHideNewTelephone();
                break;
            case R.id.iv_xiugaishouji_newtelephone_confirm_delete://清空确认密码
                etXiugaishoujiNewtelephoneConfirm.setText("");
                break;
            case R.id.iv_newtelephone_confirm_xiugaishouji_show_or_hide://显示与隐藏确认密码
                showOrHideNewTelephoneConfirm();
                break;
            case R.id.btn_xiugaishouji_queren://确认
                changePass();
                break;
        }
    }

    /**
     * 修改密码
     */
    private void changePass() {
        //旧密码
        if (TextUtils.isEmpty(etXiugaishoujiTelephone.getText().toString())) {
            showToast("请输入旧密码");
            return;
        }
        //新密码
        if (TextUtils.isEmpty(etXiugaishoujiNewtelephone.getText().toString())) {
            showToast("请输入新密码");
            return;
        }
        //确认新密码
        if (TextUtils.isEmpty(etXiugaishoujiNewtelephoneConfirm.getText().toString())) {
            showToast("请再次输入新密码");
            return;
        }
//        //两次修改的密码不能相同
//        if (etXiugaishoujiTelephone.getText().toString().equals(etXiugaishoujiNewtelephone.getText().toString())) {
//            showToast("新密码不能和旧密码相同");
//            return;
//        }
        //两次新密码输入不一致
        if (!etXiugaishoujiNewtelephone.getText().toString().equals(etXiugaishoujiNewtelephoneConfirm.getText().toString())) {
            showToast("两次输入的密码不一致");
            return;
        }
        fromNetwork.changePass(etXiugaishoujiTelephone.getText().toString(), etXiugaishoujiNewtelephone.getText().toString())
                .compose(new RxActivityHelper<CommonReturn>().ioMain(XiuGaiMiMaActivity.this, true))
                .subscribe(new MySubscribe<CommonReturn>(mContext, true) {
                    @Override
                    protected void success(CommonReturn CommonReturn) {
                        showToast("修改成功");
                        Utils.hideSoftKeyboard(XiuGaiMiMaActivity.this, title);
                        JumpHelper.finish(mContext);
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }
}
